import sqlite3

try:
    # 连接数据库
    with sqlite3.connect('example1.db') as conn:
        cursor = conn.cursor()

        # 创建表
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                age INTEGER
            )
        ''')

        # 插入数据
        users = [("Alice", 25), ("Bob", 30), ("Charlie", 35)]
        cursor.executemany("INSERT INTO users (name, age) VALUES (?, ?)", users)

        # 查询数据
        cursor.execute("SELECT * FROM users WHERE age > 25")
        print("年龄大于 25 的用户：")
        for row in cursor.fetchall():
            print(row)

        # 更新数据
        cursor.execute("UPDATE users SET age = ? WHERE name = ?", (26, "Alice"))

        # 删除数据
        cursor.execute("DELETE FROM users WHERE age > 30")

        # 验证结果
        cursor.execute("SELECT * FROM users")
        print("\n最终用户列表：")
        for row in cursor.fetchall():
            print(row)

except sqlite3.Error as e:
    print(f"数据库错误: {e}")